package com.jaux.hotel.controller;

import com.jaux.hotel.common.Result;
import com.jaux.hotel.dto.StudentDto;
import com.jaux.hotel.pojo.Student;
import com.jaux.hotel.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 16778
 */
@Api(tags = "学生管理接口")
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @ApiOperation("列出学生信息")
    @GetMapping("/list")
    public Result<List<Student>> selectList(){
        List<Student> students = studentService.selectStudentList();
        return Result.success(students, "查询成功");
    }

    @ApiOperation("根据id查询学生信息")
    @GetMapping("/info/{stuNum}")
    public Result<Student> selectById(@PathVariable("stuNum") Long stuNum){
        Student student = studentService.selectStudentById(stuNum);
        return Result.success(student, "查询成功");
    }

    @ApiOperation("根据id删除学生信息")
    @GetMapping("/del/{stuNum}")
    public Result<String> deleteById(@PathVariable("stuNum") Long stuNum){
        studentService.deleteStudentById(stuNum);
        return Result.success(null,"删除成功");
    }

    /**
     * 新增数据
     *
     * @param student 实例对象
     * @return 实例对象
     */
    @PostMapping("/add")
    @ApiOperation(value = "学生新增", notes = "添加单个学生信息")
    public Result<String> addStudent(@RequestBody Student student){

        String info = studentService.addStudent(student);
        return Result.success(null, info);
    }

    @PutMapping("/put")
    @ApiOperation(value = "学生修改", notes = "通过id修改单个学生信息")
    public Result<String> updateStudentById(int stuNum, String name){
        String updateStudentById = studentService.updateStuentById(stuNum, name);
        return Result.success(null, updateStudentById);
    }

    //@PostMapping("/login")
    //@ApiOperation(value = "学生登录", notes = "学生登录")
    //public Result<Student> login(int stuNum, String password) {
    //    return studentService.login(stuNum, password);
    //}

    @GetMapping("/select/list/{phone}/{code}")
    @ApiOperation(value = "手机号验证码登录", notes = "手机号验证码登录")
    public Result<String> selectStudentDetailById(@PathVariable( "phone") String phone, @PathVariable("code") String code){
        return studentService.login(phone, code);
    }

    @PostMapping("/register")
    @ApiOperation(value = "用户注册", notes = "用户注册")
    public Result<String> register(@RequestBody Student student){
        return studentService.register(student);
    }

    @PutMapping("/update/password")
    @ApiOperation(value = "修改密码", notes = "修改密码")
    public Result<String> updatePassword(@RequestBody StudentDto studentDto){
        return studentService.updatePassword(studentDto);
    }
}
